/*
 * Flashlib - QB9 common Flash library
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 
package com.qb9.flashlib.audio
{
	import com.qb9.flashlib.tasks.*;
	import flash.events.Event;
	
	/**
	 * Task for playing a sound (IPlayable).
	 *
	 * <p>The task completes when the playable dispatches the SOUND_COMPLETE event.</p>
	 */
	public class Play extends Task
	{
		protected var _playable:IPlayable;
		public function get playable():IPlayable {return _playable}

		public function Play(playable:IPlayable)
		{
			this._playable = playable;
		}
	
		/** @inheritDoc */
		public override function start():void
		{
			_playable.addEventListener(Event.SOUND_COMPLETE, onComplete);
			if (_playable.paused)
				_playable.resume();
			else
				_playable.play();
			super.start();
		}

		/** @inheritDoc */
		public override function stop():void
		{
			super.stop();
			_playable.pause();
			_playable.removeEventListener(Event.SOUND_COMPLETE, onComplete);
		}
		
		protected function onComplete(ev:Event):void
		{
			this.taskComplete();
		}

		/** @inheritDoc */
		public override function clone():ITask
		{
			return new Play(_playable);
		}
	}
}
